﻿using SwenAssigment.Classes;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data.SqlClient;
using System.Linq;
using System.Web;

namespace SwenAssigment.DBClasses
{
    public class OReport
    {
        List<Record> AllRecord;
        int year;
        int TotalRoom;
        public OReport(int year)
        {
            this.year = year;
            //this.TotalRoom = TotalRoom;
            AllRecord = new List<Record>();
            SqlConnection conn = new SqlConnection();
            conn.ConnectionString = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
            conn.Open();
            SqlCommand comm = new SqlCommand();
            comm.Connection = conn;
            //where year(Checkin_DateTime)>=" + year + "and year(Actual_CheckOut_DateTime)<="+year
            comm.CommandText = "select Room_Booking_ID,Checkin_DateTime,ISNULL(Actual_CheckOut_DateTime,getdate()), (select count(*) from Room) as TotalRoom from Room_Booking";
            SqlDataReader dr = comm.ExecuteReader();
            while (dr.Read())
            {
                Record bk = new Record();
                bk.id = (int)dr[0];
                bk.checkin = (DateTime)dr[1];
                bk.checkout = (DateTime)dr[2];
                this.TotalRoom = (int)dr[3];
                AllRecord.Add(bk);
            }

        }
        public Year GetYearAndDate()
        {
            //List<DateTime> AllDate = new List<DateTime>();
            List<Day> AllDay = new List<Day>();
            foreach (DateTime k in GenDate.AllDateInYear(year))
            {
                //Day TemDay = new Day(s,TotalRoom,);
                int ORoomCount = 0;
                foreach (Record r in AllRecord)
                {
                    DateTime start = new DateTime(r.checkin.Year, r.checkin.Month, r.checkin.Day, 0, 0, 0);
                    DateTime end = new DateTime(r.checkout.Year, r.checkout.Month, r.checkout.Day, 0, 0, 0);
                    if (DateTime.Compare(k, start) >= 0 && DateTime.Compare(k, end) <= 0)
                    {
                        ORoomCount++;
                    }
                }
                Day newDay = new Day(k, TotalRoom, ORoomCount);
                AllDay.Add(newDay);
            }
            Year newYear = new Year(year, TotalRoom, AllDay);
            return newYear;
        }
        public List<Week> GetAllWeek()
        {
            List<Week> AllWeek = new List<Week>();
            List<WeekDate> AllWeekInYear = GenDate.AllWeekAndDate(year);
            foreach (WeekDate s in AllWeekInYear)
            {
                List<Day> TemDay = new List<Day>();
                foreach (DateTime k in s.date)
                {
                    int ORoomCount = 0;
                    foreach (Record r in AllRecord)
                    {
                        DateTime start = new DateTime(r.checkin.Year, r.checkin.Month, r.checkin.Day, 0, 0, 0);
                        DateTime end = new DateTime(r.checkout.Year, r.checkout.Month, r.checkout.Day, 0, 0, 0);
                        if (DateTime.Compare(k, start) >= 0 && DateTime.Compare(k, end) <= 0)
                        {
                            ORoomCount++;
                        }
                    }
                    Day newDay = new Day(k, TotalRoom, ORoomCount);
                    TemDay.Add(newDay);
                }
                Week TemWeek = new Week(s.WeekNo, TotalRoom, TemDay);
                AllWeek.Add(TemWeek);
            }
            return AllWeek;
        }
        public List<Month> GetAllMonthDate()
        {
            List<Month> AllMonth = new List<Month>();
            for (int i = 1; i != 13; i++)
            {
                int MonthNo = i;
                List<DateTime> AllDate = GenDate.AllDatesInAMonth(i, year);
                List<Day> AllDay = new List<Day>();
                foreach (DateTime k in AllDate)
                {
                    int ORoomCount = 0;
                    foreach (Record r in AllRecord)
                    {
                        DateTime start = new DateTime(r.checkin.Year, r.checkin.Month, r.checkin.Day, 0, 0, 0);
                        DateTime end = new DateTime(r.checkout.Year, r.checkout.Month, r.checkout.Day, 0, 0, 0);
                        if (DateTime.Compare(k, start) >= 0 && DateTime.Compare(k, end) <= 0)
                        {
                            ORoomCount++;
                        }
                    }
                    Day tempdate = new Day(k, TotalRoom, ORoomCount);
                    //tempdate.GetStatics();
                    AllDay.Add(tempdate);
                }
                Month TemMonth = new Month(MonthNo, TotalRoom, AllDay);
                AllMonth.Add(TemMonth);
            }
            return AllMonth;
        }

    }
}